const express=require('express');
const components=require('./map.js');
const dayjs=require('dayjs');
const router=express.Router();

router.use((req,res,next)=>{
    console.log("%s %s %s",req.url,req.method,dayjs().format('YYYY-MM-DD HH:mm:ss'));
    next();
})

Object.keys(components).forEach((path)=>{
    const route=router.route(path);
    const properties=components[path];
    Object.keys(components[path]).forEach((method)=>{
        route[method]((req,res,next)=>{
            properties[method](req,res,next).catch(e=>{
                res.json({success:false,msg:e.message});
            })
        })
    })
})

module.exports=router
